import { MenuItem } from 'primeng/primeng';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from "rxjs/operators";
import { Observable, of } from 'rxjs';
import { tap, delay } from 'rxjs/operators';
@Injectable()
export class CommonService {
    isLoggedIn = false;
    redirectUrl: string;

    constructor(public http: HttpClient) { }

    login(): Observable<boolean> {
        return of(true).pipe(
            delay(300),
            tap(val => this.isLoggedIn = true)
        );
    }

    logout(): void {
        this.isLoggedIn = false;
    }

    /** 根据系统获取菜单 */
    public initsystemresource(systemId : number, resTypes =""){
        return this.http.get('/system/api/initsystemresource?sysId='+systemId+"&resTypes="+resTypes).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 获取附件信息 */
    public loadMediaDetail(id : number){
        return this.http.get('/media/api/detail/'+id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 根据地址获取菜单名称和图像 */
    public getInitSystemByUrl(url : string) {
        return this.http.get('/system/api/info/getinitsystembyurl?url=' + url).pipe(
            map((res: Response) => res['data'])
        )
    }

     /** 根据系统获取菜单 **/
    public getMenusBySystem(systemId : number, systemName : string) {
        return this.http.get('/assets/data/menu/'+systemName+'.json').pipe(
            map((res: Response) => res['data'])
        );
    }
}
